<template>
	<view class="test-wrapper">
		<view class="header">
			考试时间 <u-count-down class="ti" :time="1 * 60 * 60 * 1000" format="HH:mm:ss"></u-count-down>
		</view>
		<!-- 		<view class="you" v-if="testdata=='该试卷还没有题目' || '你已经考过了'">
			该试卷还没有题目
		</view>v-else -->
		<!-- 问答 -->
		<view class="you">
			<view class="box">
				<template v-for="it in tabs">
					<view class="left" v-if="it.type == q.type">{{it.tab}}</view>
				</template>
				<view class="right">
					{{'第' +current +'题'}}
				</view>
			</view>
			<view class="titlebox">
				<view v-html="q.title" class="title"></view>
				<view class="daan">
					<!-- 问答 -->
					<view class="boxxx" v-if="q.type == 'answer'">
						<u--textarea border height="130" v-model="q.user_value[0]" placeholder="请输入内容" count>
						</u--textarea>
					</view>
					<!-- 单选 -->
					<view class="boxxx" v-else-if="q.type == 'trueOrfalse' || q.type == 'radio'">
						<u-radio-group :checked="q.user_value == index" placement="column" @change="handleDo">
							<u-radio :customStyle="{marginBottom: '8px'}" v-for="(it, ind) in q.options" :key="ind"
								:index="ind" :label="it">
							</u-radio>
						</u-radio-group>
					</view>

					<!-- 填空 -->
					<view class="boxxx" v-if="q.type == 'completion'">
						<u--textarea border height="80" v-model="q.user_value[index]" placeholder="请输入答案..." count>
						</u--textarea>
						<view class="btn">再填一空</view>
					</view>

					<!-- 判断 -->
					<view class="boxxx" v-else-if="q.type == 'checkbox'">
						<u-radio-group v-model="radiovalue1" placement="column" @change="handleDo">
							<u-radio :customStyle="{marginBottom: '8px'}" v-for="(it, ind) in q.options" :key="ind"
								:checked="index | formatChecked(q.user_value)" :index="ind" :label="it">
							</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="box-item">
				<u-icon name="arrow-leftward" color="#333333" size="23" @click="onPage"></u-icon>
			</view>
			<view class="box-item">
				<u-icon name="grid" color="#333333" size="23"></u-icon>
				<view class="i">
					<span style="color: #5ccc84;">{{current}}</span>/{{total}}
				</view>
			</view>
			<view class="box-item" @click="show = true">
				<u-icon name="checkbox-mark" color="#333333" size="23"></u-icon>
				交卷
			</view>
			<view class="box-item">
				<u-icon name="arrow-rightward" color="#333333" size="23" @click="next(current)"></u-icon>
			</view>
		</view>
		<u-modal :show="show" content='你现在是否要交卷？' showCancelButton @confirm="add()" @cancel="show=false">
		</u-modal>
	</view>

</template>

<script>
	import {
		testList,
		savetList
	} from "../api/test.js"
	export default {
		data() {
			return {
				show: false,
				current: 1,
				tabs: [{
						type: "answer",
						tab: "问答题"
					}, {
						type: "completion",
						tab: "填空题"
					},
					{
						type: "trueOrfalse",
						tab: "判断题"
					},
					{
						type: "checkbox",
						tab: "多选题"
					}, {
						type: "radio",
						tab: "单选选题"
					},
				],
				id: "",
				uid: "",
				testdata: [],
				total: 0,
				user_test_id: 0,
				isback: false,
				value: []
			}
		},
		onLoad(e) {
			console.log(e);
			this.id = e.id
			this.getList()
		},
		computed: {
			// 当前题目
			q() {
				return this.testdata[this.current - 1] || {};
			},
			// 判断哪些没有填的题目
			// undo() {
			// 	let arr = [];
			// 	this.testdata.forEach((item, index) => {
			// 		if (
			// 			((item.type == "answer" || item.type == "completion") &&
			// 				!item.user_value[0]) ||
			// 			((item.type == "trueOrfalse" || item.type == "radio") &&
			// 				item.user_value == -1) ||
			// 			(item.type == "checkbox" && item.user_value.length == 0)
			// 		) {
			// 			arr.push(index + 1);
			// 		}
			// 	});
			// 	return arr;
			// },
		},
		methods: {

			//提交
			async add() {
				const res = await savetList({
					value: this.value,
					user_test_id: this.uid
				})
				console.log(res,"222")
			},
			//上一题
			onPage() {
				if (this.current <= 1) {
					return
				}
				this.current = this.current - 1
			},
			// 下一题
			next() {
				if (this.current >= this.total) {
					return
				}
				this.current = this.current + 1
			},

			async getList() {
				const res = await testList({
					id: this.id
				})
				console.log(res, "ididid")
				this.uid = res.data.data.user_test_id
				this.testdata = res.data.data.testpaper_questions
				this.total = this.testdata.length
			}
		}
	}
</script>

<style lang="scss">
	.test-wrapper {
		width: 100%;
		height: 100%;

		.footer {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
			height: 100rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.box-item {
				width: 25%;
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				font-size: 35rpx;
			}
		}

		.header {
			width: 100%;
			height: 100rpx;
			border-top: red 1px solid;
			border-bottom: red 1px solid;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 40rpx;
			color: red;

			.u-count-down__text {
				margin-left: 10rpx;
				font-size: 40rpx;
				color: red;
			}
		}

		.you {
			width: 100%;

			.titlebox {
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 20rpx;
				box-sizing: border-box;

				.title {
					width: 100%;
					height: 50px;
					font-size: 32rpx;
					color: #333333;
					display: flex;
					align-items: center;
				}

				.daan {
					width: 100%;
					height: 100%;

					.boxxx {
						.btn {
							margin-top: 20rpx;
							height: 90rpx;
							width: 90%;
							border-radius: 10rpx;
							font-size: 33rpx;
							display: flex;
							justify-content: center;
							align-items: center;
							color: white;
							background-color: #5ccc84;
						}
					}


				}
			}

			.box {
				width: 100%;
				height: 50rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				box-sizing: border-box;

				.left {
					color: #333333;
					font-size: 31rpx;
				}

				.right {
					color: #999999;
					font-size: 28rpx;
				}
			}
		}

	}
</style>
